// pages/report/report.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serviceDesc: '',
    orderId: null,
    imageSrc: ['../../icons/jia.png'], // 初始显示预设图片
    imageNum: 0,
    content: '',
    detailPics: []
  },





  /**
   * 获取文本内容
   */
  text: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  /**
   * 上传图片
   */
  uploadDetailImage: function (e) { //这里是选取图片的方法
    var that = this;
    var pics = [];
    var detailPics = that.data.detailPics;
    if (detailPics.length >= 9) {
      wx.showToast({
        title: '最多选择' + 9 + '张！',
        icon: "none"
      })
      return;
    }
    wx.chooseImage({
      count: 9 - this.data.detailPics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgs = res.tempFilePaths;
        console.log(res);
        console.log(res.tempFileSize)
        // if (res.tempFileSize < 3000000) {
        console.log(imgs);
        pics = imgs;
        for (var i = 0; i < pics.length; i++) {
          if (res.tempFiles[i].size > 3000000) {
            wx.showToast({
              title: '图片上传失败',
              icon: "none"
            })
          }
        }
        // } else {
        //   wx.showToast({
        //     title: '图片上传失败',
        //     icon: "none"
        //   })
        // }
        console.log(pics)

        // console.log(that.data.detailPics);
        for (let i = 0; i < pics.length; i++) {
          wx.uploadFile({
            url: 'https://www.xiaozheng8.com/upload',
            filePath: pics[i],
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data'
            },
            success: function (res) {

              that.data.detailPics.push(res.data);
              console.log(res.data);
              that.setData({
                detailPics: that.data.detailPics
              })
              if (that.data.detailPics.length > 9) {
                wx.showToast({
                  title: '最多选择' + 9 + '张！',
                  icon: "none"
                })
                that.data.detailPics.splice(9, that.data.detailPics.length - 9)
                that.setData({
                  detailPics: that.data.detailPics
                })
              }
              console.log(that.data.detailPics)

            }

          })

        }

        console.log(that.data.detailPics)

      },
    })

  },


  cancel: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.data.detailPics.splice(index, 1);
    this.setData({
      detailPics: this.data.detailPics
    })
  },
  // upload: function () {
  //       let that = this;
  //       wx.chooseImage({
  //             sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
  //             sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
  //             success: function (res) {
  //                   let url = res.tempFilePaths,
  //                         num = res.tempFilePaths.length;
  //                   for (let i = 0; i < num; i++) {
  //                         wx.uploadFile({
  //                               url: 'https://www.xiaozheng8.com/upload',
  //                               filePath: url[i],
  //                               name: 'file',
  //                               header: {
  //                                     'Content-Type': 'multipart/form-data'
  //                               },
  //                               success: function (res) {
  //                                     console.log(url[i])
  //                                     that.data.imageSrc[i] = res.data;
  //                                     that.setData({
  //                                           imageSrc: that.data.imageSrc
  //                                     })
  //                               }
  //                         })
  //                   }
  //                   console.log(that.data.imageSrc)

  //             },
  //             fail: function (res) {
  //               wx.showToast({
  //                 title: '图片上传失败',
  //                 icon:"none",
  //               })
  //             }
  //       })
  // },

  /**
   * 删除图片事件函数
   */
  // cancel: function (e) {
  //       let arr = this.data.imageSrc;
  //       let a = e.currentTarget.dataset.index
  //       this.data.imageSrc[a] = ''
  //       this.setData({
  //             imageSrc: this.data.imageSrc
  //       })
  // },
  // cancel: function (e) {
  //   let arr = this.data.imageSrc;
  //   let a = e.currentTarget.dataset.index
  //   var that = this;
  //   if (this.data.imageSrc.length > 1) {
  //     that.data.imageSrc.splice(a, 1);
  //     that.setData({
  //       imageSrc: that.data.imageSrc
  //     })
  //   } else {
  //     that.data.imageSrc[a] = "../../icons/jia.png",
  //       that.setData({
  //         imageSrc: that.data.imageSrc
  //       })
  //   }


  // },


  /**
   * 提交报告
   */
  submit: function () {
    let that = this;
    console.log(that.data.imageSrc);
    console.log(that.data.content)
    if (that.data.content == '') {
      wx.showModal({
        title: "提示",
        content: '请输入施工描述',
        showCancel: false,
        confirmText: '确认',
        confirmColor: '#0bbd79',
      })
    } else if (that.data.detailPics == '') {
      wx.showModal({
        title: "提示",
        content: '请上传图片',
        showCancel: false,
        confirmText: '确认',
        confirmColor: '#0bbd79',
      })
    } else {
      wx.showModal({
        title: "提示",
        content: '确认提交报告',
        cancelText: '取消',
        cancelColor: '#999',
        confirmText: '确认',
        confirmColor: '#0bbd79',
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v3',
              method: 'post',
              data: {
                status: 9,
                orderId: that.data.orderId,
                userId: wx.getStorageSync("userId"),
                img: that.data.detailPics,
                content: that.data.content,
                des: that.data.serviceDesc
              },
              success: function (res) {
                wx.redirectTo({
                  url: '../grabSuccess/grabSuccess?orderId=' + that.data.orderId,
                })
                console.log('--报告上传成功-')
                console.log(res)
              },
            })
          } else { }

        }
      })

    }

  },

  preview(e) {
    var that = this;
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.detailPics[index],
      urls: that.data.detailPics,

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId,
      serviceDesc: options.serviceDesc
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})